<template>
  <!-- 企业微信基础设置 -->
  <div v-loading="compLoad">
    <!-- 企业信息 -->
    <div>
      <div
        v-for="(item, key) in enterpriseInfo"
        :key="key"
        style="margin-bottom: 15px"
      >
        <div class="flex_y_center">
          <div style="font-weight: 600">{{ key }}：</div>
          <div>{{ item }}</div>
        </div>
      </div>
    </div>
    <div>
      <div style="font-size: 20px; font-weight: 600">应用</div>
      <hr />
      <!-- 基础功能选项 -->
      <div>
        <div style="color: rgba(0, 0, 0, 0.5)">基础</div>
        <!-- 功能选项 -->
        <div class="grid_4_column" style="margin-top: 10px">
          <!-- 应用项 -->
          <div
            v-for="(item, index) of appItems"
            :key="index"
            class="btn_items"
            @click="btnClick(item)"
          >
            <div>
              <div class="def_style">
                {{ item.defaultSrc }}
              </div>
            </div>
            <div class="title_style">{{ item.app_name }}</div>
          </div>
          <!-- 添加 -->
          <div class="btn_items" @click="btnClick">
            <div>
              <div
                class="def_style"
                style="background-color: rgba(0, 0, 0, 0.1)"
              >
                <i class="el-icon-plus"></i>
              </div>
            </div>
            <div class="title_style">添加功能</div>
          </div>
        </div>
      </div>
    </div>

    <!-- 通用对话框 -->
    <el-dialog :title="dialogTitle" :visible.sync="universalDialog">
      <div>
        <!-- 绑定微信客服 -->
        <Wei-xin-service
          v-if="dialogTitle == '绑定微信客服'"
          :o-api-url="apiUrl"
          @send="closeDialog"
        />
        <Add-app v-else-if="dialogTitle == '添加应用'" @send="closeDialog" />
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { getSetting, getApp } from '@/api/qw'

  export default {
    components: {
      WeiXinService: () => import('./components/weixinservice.vue'),
      AddApp: () => import('./components/addapp.vue'),
    },
    data() {
      return {
        compLoad: false,
        enterpriseInfo: {}, // 存放当前企业信息

        appItems: [], // 存放应用列表

        dialogTitle: '', // dialog标题
        apiUrl: '', // 存放当前选中的应用功能的api_user
        universalDialog: false, // 控制通用dialog显示隐藏
      }
    },
    created() {
      this.getSettingList()
      this.getAppList()
    },
    methods: {
      // 获取公司信息
      getSettingList() {
        this.compLoad = true
        getSetting().then((res) => {
          this.enterpriseInfo = {
            公司名称: res.data.corp_name,
            corp_id: res.data.corp_id,
            corp_code: res.data.corp_code,
          }
          this.compLoad = false
        })
      },
      // 获取应用列表
      getAppList() {
        getApp().then((res) => {
          this.appItems = res.data
          for (let item of this.appItems) {
            // 判断当前是否有自带图片，没有的话默认设置第一个字
            item.defaultSrc = item.app_name.slice(0, 1)
          }
        })
      },
      // 功能按钮点击事件
      btnClick(item) {
        if (item) {
          this.apiUrl = item.api_url
        }
        switch (item.app_name) {
          case '微信客服':
            this.dialogTitle = '绑定微信客服'
            break
          default:
            this.dialogTitle = '添加应用'
            break
        }
        this.universalDialog = true
      },
      // 初始化dialog
      closeDialog() {
        this.universalDialog = false
        this.dialogTitle = ''
      },
    },
  }
</script>

<style scoped>
  .grid_4_column {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    grid-gap: 10px;
  }

  .btn_items {
    padding: 10px;
    border-radius: 10px;
    border: 1px solid rgba(0, 0, 0, 0.2);
    box-sizing: border-box;
    cursor: pointer;
    transition: 0.5s;
    display: flex;
    align-items: center;
  }
  .btn_items:hover {
    background-color: rgba(204, 204, 204, 0.137);
  }
  .img_style,
  .add_style {
    width: 50px;
    height: 50px;
  }
  .def_style {
    width: 50px;
    height: 50px;
    display: flex;
    justify-content: center;
    align-items: center;
    color: white;
    font-size: 22px;
    font-weight: 600;
    background-color: #2ebb96;
    border-radius: 5px;
  }
  .title_style {
    font-size: 16px;
    font-weight: 600;
    margin-left: 15px;
  }
  .introduce_style {
    font-size: 11px;
    color: rgba(0, 0, 0, 0.5);
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 1;
  }
</style>
